/**
 * 热门搜索
 * @author Qasim <15750783791@163.com>
 * @version 2016/10/21
 */
document.addEventListener('plusready' , ready , false);
 
var self = '';
var tid = '';
function ready(){
	self = plus.webview.currentWebview();
	tid = self.tid;
	searchResult(self.keywords);
}

/**
 * 搜索结果
 * @param {String} keywords
 */
function searchResult(keywords){
	plus.nativeUI.showWaiting('搜索中...');
	console.log(apiRoot + '/Home/Article/searchData/tid/'+tid+'/keywords/'+keywords) 
	$.ajax({
		type :'post',
		url : apiRoot + '/Home/Article/searchData',
		data : {tid : tid , keywords : keywords},
		dataType : 'json',
		success : function(data){
			plus.nativeUI.closeWaiting();
			if(data != ''){ 
				var html = '<div></div><ul class="mui-table-view">';
				$.each(data, function(k , v) {
					if(v.tid == 33){ 
						html += '<li class="mui-table-view-cell mui-media info" data-id = "'+v.id+'" data-tid = "'+v.tid+'" data-level="'+v.level+'" data-scale ="'+v.scale+'" data-addr ="'+v.address+'" data-details="'+v.details+'" data-contacts="'+v.contacts+'" data-phone="'+v.phone+'">';
						html += '<img class="mui-media-object mui-pull-left" style="height:63px;" src="'+getImgUrl(v.litpic)+'">';
						html += '<div class="mui-media-body"><span class="title">'+v.title+'</span>';
						html += '<p><span class="iconfont icon-xingxing color-zx-2"></span>'; 
						html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 1 ? "" : "color-c")+'"></span>'; 
						html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 2 ? "" : "color-c")+'"></span>'; 
						html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 3 ? "" : "color-c")+'"></span>'; 
						html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 4 ? "" : "color-c")+'"></span></p>';
						html += '<span class="color-c font-12 classify">'+v.name+'</span></div></li>';
					}else{
						html += '<li class="mui-table-view-cell mui-media info" data-addr="'+v.address+'" data-phone="'+v.phone+'" data-contacts="'+v.contacts+'" data-id="'+v.id+'" data-tid="'+v.tid+'" data-company="'+v.company_name+'" data-xueli="'+v.education+'" data-number="'+v.number+'" data-exp ="'+v.experience+'" >';
						html += '<img class="mui-media-object mui-pull-left" src="'+getImgUrl(v.litpic)+'">';
						html += '<div class="mui-media-body"><span class="title">'+v.title+'</span>'; 
						html += '<p class="mui-ellipsis">'+(v.name ? v.name : '<br/>')+'</p>';
						html += '<p class="time addtime">'+v.addtime+'</p></div><div class="content" style="display:none">'+v.body+'</div></li>'; 
					}
				}); 
				html += '</ul>'; 
				$('.mui-content').html(html);
				$('.info').unbind();
				$('.info').on('click' , function(){
					var tid = $(this).attr('data-tid'); 
					if(tid == 28){
						plus.nativeUI.showWaiting('跳转中...');
						var yqhc_inf = plus.webview.create('yqhc-inf.html' , 'yqhc-inf' , {} , { 
							aid : $(this).attr('data-id') , 
							tid : tid ,
							pic : $(this).find('img').attr('src'),
							title : $(this).find('.title').text(),
							addtime : $(this).find('.addtime').text(),
							company : $(this).attr('data-company'),
							contacts : $(this).attr('data-contacts'),
							phone : $(this).attr('data-phone'),
							body : $(this).find('.content').html()
	
						});
						yqhc_inf.addEventListener('loaded', function (){
							yqhc_inf.show('pop-in');
							plus.nativeUI.closeWaiting();
						})
					}else if(tid == 29){
						plus.nativeUI.showWaiting('跳转中...');
						var jsfw_inf = plus.webview.create('jsfw-inf-1.html' , 'jsfw-inf-1' , {} , {
							aid : $(this).attr('data-id') , 
							tid : tid,
							pic : $(this).find('img').attr('src'),
							title : $(this).find('.title').text(),
							addtime : $(this).find('.addtime').text(),
							company : $(this).attr('data-company'),
							contacts : $(this).attr('data-contacts'),
							phone : $(this).attr('data-phone'),
							body : $(this).find('.content').html()
	
						});
						jsfw_inf.addEventListener('loaded', function (){
							jsfw_inf.show('pop-in');
							plus.nativeUI.closeWaiting();
						})
					}else if(tid == 26){
						plus.nativeUI.showWaiting('跳转中...');
						var hy_inf = plus.webview.create('hy-inf.html' , 'hy-inf' , {} , {
							aid : $(this).attr('data-id') , 
							tid : tid, 
							title : $(this).find('.title').text(),
							body : $(this).find('.content').html(),
						});
						hy_inf.addEventListener('loaded', function (){
							hy_inf.show('pop-in');
							plus.nativeUI.closeWaiting();
						})
					}else if(tid == 4){ 
						plus.nativeUI.showWaiting('跳转中...');
						var hd_inf = plus.webview.create('hd-inf.html' , 'hd-inf' , {} , {
							tid : tid ,
							phone : $(this).attr('data-phone'), 
							title : $(this).find('.title').text(),
							body : $(this).find('.content').html() ,
							aid : $(this).attr('data-id')
						});
						hd_inf.addEventListener('loaded', function (){
							hd_inf.show('pop-in');
							plus.nativeUI.closeWaiting();
						})	
					}else if(tid == 27){
						plus.nativeUI.showWaiting('跳转中...');
						var qzzp_inf = plus.webview.create('qzzp-inf.html' , 'qzzp-inf' , {} , { 
							aid : $(this).attr('data-id') , 
							tid : tid,
							title : $(this).find('.title').text(),
							addtime : $(this).find('.addtime').text(),
							salary : $(this).find('.salary').text(),
							company : $(this).attr('data-company'),
							education : $(this).attr('data-xueli'),
							number :  $(this).attr('data-number'),
							experience : $(this).attr('data-exp'),
							contacts : $(this).attr('data-contacts'),
							phone : $(this).attr('data-phone'),
							address : $(this).attr('data-addr'),
							body : $(this).find('.content').html()
						});
						
						qzzp_inf.addEventListener('loaded', function (){
							qzzp_inf.show('pop-in');
							plus.nativeUI.closeWaiting();
						})	
					}else if(tid == 30){
						plus.nativeUI.showWaiting('跳转中...');
						var bbs_inf = plus.webview.create('bbs-inf.html', 'bbs-inf', {}, {
							avatar : $(this).find('img').attr('src'),
							nickname : $(this).find('.nickname').text(),
							title : $(this).find('.title').text(),
							addtime : $(this).find('.addtime').text(),
							aid : $(this).attr('data-id'),
							tid : $(this).attr('data-tid'),
							body : $(this).find('.content').html()
						}); 
						bbs_inf.addEventListener('loaded', function (){
							bbs_inf.show('pop-in');
							plus.nativeUI.closeWaiting();
						})	
					}else if(tid == 1){
						plus.nativeUI.showWaiting('跳转中...');
						var use_rule =  plus.webview.create('user-text.html' , 'user-text' , {} , {
							body : $(this).find('.content').html()
						});
						use_rule.addEventListener('loaded', function (){
							use_rule.show('pop-in');
							plus.nativeUI.closeWaiting();
						})	
					}else if(tid == 33){
						plus.nativeUI.showWaiting('跳转中...');
						var nav_inf = plus.webview.create('nav-inf.html', 'bbs-inf', {}, {
							company_name : $(this).find('.title').text(),
							level : $(this).attr('data-level'),
							classify : $(this).find('.classify').text(),
							aid : $(this).attr('data-id'),
							tid : tid,
							body : $(this).attr('data-details'),
							scale : $(this).attr('data-scale'),
							address : $(this).attr('data-addr'),
							phone : $(this).attr('data-phone'),
							contacts : $(this).attr('data-contacts')
						});
						nav_inf.addEventListener('loaded', function (){
							nav_inf.show('pop-in');
							plus.nativeUI.closeWaiting();
						})	
					}
				})
				
			}else{
				$('.mui-content').html('<div class="mui-text-center font-14"style="margin:3em 0 2em;">您的搜索没有匹配的结果</div><div class="table-view-cell"><button class="mui-btn mui-btn-block background-zx color-white search-btn">去发布</button></div>')
				$('.search-btn').on('click' , function(){
					plus.nativeUI.showWaiting();
					var me_fb_list = plus.webview.create('me-fb-list.html', 'me_fb_list', {}, {});
					me_fb_list.addEventListener('loaded', function (){
						me_fb_list.show('pop-in');
						plus.nativeUI.closeWaiting();
					})
				}) 
			}
		},
		error: function(e){
			console.log(JSON.stringify(e))
			plus.nativeUI.closeWaiting();
			toast('网络连接失败');
		}
	})
	
}


/**
 * 搜索
 * @param {String} keywords
 */
function searchMsg(keyword){
	if(!keyword){
		toast('请输入关键字');
		return;
	}
	searchResult(keyword);
}
